package com.eclink.asset.portal.controller;


import com.eclink.asset.domain.dto.DistributeItem;
import com.eclink.asset.portal.domain.dao.PortalDataBoard;
import com.eclink.asset.portal.domain.dto.MyPortalDTO;
import com.eclink.asset.portal.domain.dto.PortalDataBoardDTO;
import com.eclink.asset.portal.domain.dto.PortalSettingDTO;
import com.eclink.asset.portal.domain.dto.StatisticsChartQuery;
import com.eclink.asset.portal.service.DataBoardService;
import com.eclink.asset.service.AssetCategoryService;
import com.eclink.asset.sys.domain.dto.UserFavoritesMenuDTO;
import com.eclink.asset.sys.service.SysMenuServiceImpl;
import com.eclink.asset.warehouse.service.WarehouseService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.domain.dto.query.RecordMenuQueryDTO;
import com.ruoyi.domain.entity.RecordMenuVisit;
import com.ruoyi.system.service.impl.SysDictDataServiceImpl;
import com.ruoyi.web.service.IndexService;
import com.ruoyi.web.service.RecordMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * 首页的相关接口
 *
 * @author chenshixian
 */
@Api(tags="门户接口")
@RestController
@RequestMapping("portal")
public class PortalController {

    @Resource
    private IndexService indexService;

    @Resource
    private RecordMenuService recordMenuService;

    @Resource
    private AssetCategoryService assetCategoryService;

    @Resource
    private WarehouseService warehouseService;

    @Resource
    private SysMenuServiceImpl sysMenuService;

    @Resource
    private SysDictDataServiceImpl dictDataService;

    @Resource
    private DataBoardService dataBoardService;

    /**
     * 获取当前用户首页的配置
     * @return 用户的首页配置信息
     */
    @GetMapping("getMyPortalSetting")
    public ResponseEntity<MyPortalDTO> getMyPortalSetting() {
        MyPortalDTO res = new MyPortalDTO();
        // 常用菜单
        res.setFavoritesMenuList(sysMenuService.selectMyFavoritesMenu());
        List<PortalDataBoard> myBoardList = dataBoardService.getMyBoardList();
        res.setDataBoardList(myBoardList);
        return ResponseEntity.ok(res);
    }

    /**
     * 统计资产的总数量
     * @return 总数量
     */
    @ApiOperation("统计资产的总数量")
    @GetMapping("dataBoard/data")
    public AjaxResult dataBoardData() {
        Map<String, Object> res = new HashMap<>();
        res.put("asset_total_count", indexService.getAssetTotalCount());
        res.put("asset_total_amount", indexService.getAssetTotalAmount());
        res.put("asset_using", indexService.getAssetCountUsing());
        res.put("asset_idle", indexService.getAssetCountIdle());
        res.put("asset_borrow", indexService.getAssetCountBorrow());
        res.put("asset_handle", indexService.getAssetCountHandle());
        res.put("asset_idle_rate", indexService.getAssetIdleRate());
        return AjaxResult.success(res);
    }

    /**
     * 资产状态的分布
     * @return 结果
     */
    @ApiOperation("统计资产状态的分布")
    @GetMapping("chart/assetDistribute")
    public AjaxResult chartAssetDistribute(StatisticsChartQuery query) {
        List<DistributeItem> list = indexService.getAssetStateDistribute(query);
        String groupBy = query.getGroupBy();
        if (groupBy.equals("state")) {
            for(DistributeItem item : list) {
                item.setName(dictDataService.selectDictLabel("asset_state", item.getName()));
            }
        } else if (groupBy.equals("category_id")) {
            for(DistributeItem item : list) {
                item.setName(assetCategoryService.selectNameById(item.getName()));
            }
        } else if (groupBy.equals("warehouse_id")) {
            for(DistributeItem item : list) {
                item.setName(warehouseService.getWarehouseNameById(item.getName()));
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 资产种类的分布
     * @return 结果
     */
    @ApiOperation("统计资产种类的分布")
    @GetMapping("chart/assetCategoryDistribute")
    public AjaxResult chartAssetCategoryDistribute() {
        List<DistributeItem> list = indexService.getAssetCategoryDistribute();
        for (DistributeItem item : list) {
            item.setName(assetCategoryService.selectNameById(item.getName()));
        }
        return AjaxResult.success(list);
    }

    /**
     * 资产仓库的分布
     * @return 结果
     */
    @ApiOperation("统计资产仓库的分布")
    @GetMapping("chart/assetWarehouseDistribute")
    public AjaxResult chartAssetWarehouseDistribute() {
        List<DistributeItem> list = indexService.getAssetWarehouseDistribute();
        for (DistributeItem item : list) {
            String name = item.getName();
            if (StringUtils.isNotBlank(name)) {
                item.setName(warehouseService.getWarehouseNameById(Long.parseLong(name)));
            } else {
                item.setName("未知");
            }

        }
        return AjaxResult.success(list);
    }

    /**
     * 耗材的领用情况
     * @return 结果
     */
    @ApiOperation("统计耗材的领用情况")
    @GetMapping("chart/consume/use")
    public AjaxResult chartConsumeUseStatistics() {
        List<DistributeItem> list = indexService.getConsumeUseStatistics();
        return AjaxResult.success(list);
    }

    /**
     * 耗材的入库的情况
     * @return 结果
     */
    @ApiOperation("统计耗材入库的情况")
    @GetMapping("chart/consume/in")
    public AjaxResult chartConsumeIn() {
        List<DistributeItem> list = indexService.getConsumeInStatistics();
        return AjaxResult.success(list);
    }


    /**
     * 记录菜单的访问
     * @return 操作结果
     */
    @PostMapping("recordMenu")
    public AjaxResult recordMenu(@RequestBody RecordMenuQueryDTO query)
    {
        recordMenuService.recordMenuVisit(query);
        return AjaxResult.success(1);
    }

    /**
     * 获取菜单的访问记录
     * @return 操作结果
     */
    @GetMapping("getMenuRecord")
    public AjaxResult getMenuRecord()
    {
        Long userId = SecurityUtils.getUserId();
        List<RecordMenuVisit> res = recordMenuService.getMenuRecord(userId);
        return AjaxResult.success(res);
    }


}
